package com.avic.rong.course.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.course.activity.DealActivity;
import com.avic.rong.course.bean.CompanysBean;
import com.avic.rong.course.bean.PublishGYBean;
import com.avic.rong.course.bean.PublishXQBean;
import com.avic.rong.course.showapi.publishDelApi;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.questionOrAnswer.api.DeleteQuestionApi;
import com.avic.rong.userOrder.bean.AllOrderListBean;
import com.avic.rong.userOrder.event.InformUpdate;
import com.avic.rong.userOrder.viewHolder.OrderListViewHolder;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.DialogCustom;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.example.minato.minastore.utils.AppUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

import static com.avic.rong.R2.id.brightness_progressbar;
import static com.avic.rong.R2.id.del;
import static com.avic.rong.R2.id.edit;
import static com.avic.rong.R2.id.look;
import static com.avic.rong.R2.id.numIndicator;
import static com.avic.rong.R2.id.tradeMessage;

/**
 * Created by XinEr on 2019/7/17.
 * Describe:
 */
public class PublishListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<PublishGYBean> gylistData;
    private List<PublishXQBean> xqlistData;
    private String fabuname;
    private long id;

    public PublishListAdapter(Context context) {
        this.context = context;
    }

    public void setListGYData(List<PublishGYBean> gylistData, String fabuname) {
        this.gylistData = gylistData;
        this.fabuname = fabuname;
        notifyDataSetChanged();
    }

    public void setListXQData(List<PublishXQBean> xqlistData, String fabuname) {
        this.xqlistData = xqlistData;
        this.fabuname = fabuname;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder = null;
        if ("发布的需求".equals(fabuname)) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.fragment_fbxq, viewGroup, false);
            holder = new PublishXQViewHolder(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.fragment_fbgy, viewGroup, false);
            holder = new PublishGYViewHolder(inflate);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if ("发布的需求".equals(fabuname)) {
            if (xqlistData != null && xqlistData.size() > 0) {
                PublishXQBean publishXQBean = xqlistData.get(position);
//                skuId = publishXQBean.getSkuId();
                id = publishXQBean.getId();
                Log.e("================", publishXQBean.getContacts());
                ((PublishXQViewHolder) holder).xqtitle.setText(publishXQBean.getTitle().split("T")[0]);
                ((PublishXQViewHolder) holder).xqstartTime.setText(publishXQBean.getPublishTime().split("T")[0]);
                ((PublishXQViewHolder) holder).xqendTime.setText(publishXQBean.getEndTime());
                ((PublishXQViewHolder) holder).xqdel.setOnClickListener(new MyXQOnClickListener((PublishXQViewHolder) holder, publishXQBean, position));
                ((PublishXQViewHolder) holder).xqedit.setOnClickListener(new MyXQOnClickListener((PublishXQViewHolder) holder, publishXQBean, position));
                ((PublishXQViewHolder) holder).xqlook.setOnClickListener(new MyXQOnClickListener((PublishXQViewHolder) holder, publishXQBean, position));
                if (1 == publishXQBean.getStatus()) {
                    ((PublishXQViewHolder) holder).xqstatus.setText("审核中");
                    showXQ(((PublishXQViewHolder) holder), View.GONE, View.GONE, View.VISIBLE);
                } else if (2 == publishXQBean.getStatus()) {
                    ((PublishXQViewHolder) holder).xqstatus.setText("对接中");
                    showXQ(((PublishXQViewHolder) holder), View.VISIBLE, View.VISIBLE, View.VISIBLE);
                } else if (11 == publishXQBean.getStatus()) {
                    ((PublishXQViewHolder) holder).xqstatus.setText("未通过");
                    showXQ(((PublishXQViewHolder) holder), View.VISIBLE, View.VISIBLE, View.VISIBLE);
                } else if (99 == publishXQBean.getStatus()) {
                    ((PublishXQViewHolder) holder).xqstatus.setText("已关闭");
                    showXQ(((PublishXQViewHolder) holder), View.VISIBLE, View.GONE, View.VISIBLE);
                }
            }
        } else {
            if (gylistData != null && gylistData.size() > 0) {
                PublishGYBean publishGYBean = gylistData.get(position);
                int skuId = publishGYBean.getSkuId();
                id = publishGYBean.getId();
                ((PublishGYViewHolder) holder).title.setText(publishGYBean.getTitle());
                ((PublishGYViewHolder) holder).contact.setText("联系人：" + publishGYBean.getContacts());
                ((PublishGYViewHolder) holder).startTime.setText("发布时间：" + publishGYBean.getPublishTime().split("T")[0]);
                ((PublishGYViewHolder) holder).lookGoods.setOnClickListener(new MyGYOnClickListener((PublishGYViewHolder) holder, publishGYBean, position));
                ((PublishGYViewHolder) holder).tradeMessage.setOnClickListener(new MyGYOnClickListener((PublishGYViewHolder) holder, publishGYBean, position));
                ((PublishGYViewHolder) holder).del.setOnClickListener(new MyGYOnClickListener((PublishGYViewHolder) holder, publishGYBean, position));
                ((PublishGYViewHolder) holder).edit.setOnClickListener(new MyGYOnClickListener((PublishGYViewHolder) holder, publishGYBean, position));
                ((PublishGYViewHolder) holder).look.setOnClickListener(new MyGYOnClickListener((PublishGYViewHolder) holder, publishGYBean, position));
                Glide.with(context).load(publishGYBean.getFiles()).error(R.mipmap.doc_default).into(((PublishGYViewHolder) holder).img);
                if (1 == publishGYBean.getStatus()) {
                    ((PublishGYViewHolder) holder).status.setText("审核中");
                    showGY(((PublishGYViewHolder) holder), View.GONE, View.GONE, View.GONE, View.GONE, View.VISIBLE);
                } else if (2 == publishGYBean.getStatus()) {
                    ((PublishGYViewHolder) holder).status.setText("对接中");
                    showGY(((PublishGYViewHolder) holder), View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE);
                } else if (11 == publishGYBean.getStatus()) {
                    ((PublishGYViewHolder) holder).status.setText("未通过");
                    showGY(((PublishGYViewHolder) holder), View.GONE, View.GONE, View.VISIBLE, View.VISIBLE, View.VISIBLE);
                } else if (99 == publishGYBean.getStatus()) {
                    ((PublishGYViewHolder) holder).status.setText("已关闭");
                    showGY(((PublishGYViewHolder) holder), View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE);
                }
            }
        }

    }

    private void showGY(PublishGYViewHolder holder, int lookGoods, int tradeMessage, int del, int edit, int look) {
        holder.lookGoods.setVisibility(lookGoods);
        holder.tradeMessage.setVisibility(tradeMessage);
        holder.del.setVisibility(del);
        holder.edit.setVisibility(edit);
        holder.look.setVisibility(look);
    }

    private void showXQ(PublishXQViewHolder holder, int del, int edit, int look) {
        holder.xqdel.setVisibility(del);
        holder.xqedit.setVisibility(edit);
        holder.xqlook.setVisibility(look);
    }

    @Override
    public int getItemCount() {
        if ("发布的需求".equals(fabuname)) {
            return xqlistData == null ? 0 : xqlistData.size();
        } else {
            return gylistData == null ? 0 : gylistData.size();
        }
    }

    class MyGYOnClickListener implements View.OnClickListener {

        PublishGYViewHolder holder;
        PublishGYBean bean;
        int position;

        public MyGYOnClickListener(PublishGYViewHolder holder, PublishGYBean bean, int position) {
            this.holder = holder;
            this.bean = bean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.del:
                    delDialog(bean, null);
                    break;
                case R.id.edit:
                    ToastTools.showPublicToast("请到PC端进行该操作");
                    break;
                case R.id.tradeMessage:
                    Intent intent = new Intent(context, DealActivity.class);
                    intent.putExtra("skuId", bean.getSkuId());
                    context.startActivity(intent);
                    break;
            }
        }
    }

    class MyXQOnClickListener implements View.OnClickListener {

        PublishXQViewHolder holder;
        PublishXQBean bean;
        int position;

        public MyXQOnClickListener(PublishXQViewHolder holder, PublishXQBean bean, int position) {
            this.holder = holder;
            this.bean = bean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.xqdel:
                    delDialog(null, bean);
                    break;
                case R.id.xqedit:
                    ToastTools.showPublicToast("请到PC端进行该操作");
                    break;
            }
        }
    }

    private void delDialog(PublishGYBean gybean, PublishXQBean xqbean) {
        DialogCustom.showDoubleDialog(context, "", "确定要删除吗？", "取消", "确定", new DialogCustom.CustomDialogDouble() {
            @Override
            public void rightButtonClicked() {
                final publishDelApi publishDelApi = new publishDelApi((RxAppCompatActivity) context, simpleOnNextListenerDeleteQue);
                publishDelApi.setCache(false);
                if (null != gybean) {
                    publishDelApi.setArgument("supply 供应", gybean.getSupplyId());
                    gylistData.remove(gybean);
                    notifyDataSetChanged();
                } else {
                    publishDelApi.setArgument("需求", xqbean.getDemandId());
                    xqlistData.remove(xqbean);
                    notifyDataSetChanged();
                }
                HttpManager.getInstance().doHttpDeal(publishDelApi);

            }

            @Override
            public void leftButtonClicked() {

            }
        });
    }


    //删除
    HttpOnNextListener simpleOnNextListenerDeleteQue = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_1000)) {
                ToastTools.showToast(context, subjects.getMsg());
                notifyDataSetChanged();
                return;
            }

            ToastTools.showPublicToast("删除成功");
        }
    };


    public class PublishGYViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.status)
        TextView status;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.contact)
        TextView contact;
        @BindView(R.id.startTime)
        TextView startTime;
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.lookGoods)
        TextView lookGoods;
        @BindView(R.id.tradeMessage)
        TextView tradeMessage;
        @BindView(R.id.del)
        TextView del;
        @BindView(R.id.edit)
        TextView edit;
        @BindView(R.id.look)
        TextView look;

        public PublishGYViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class PublishXQViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.xqstatus)
        TextView xqstatus;
        @BindView(R.id.xqtitle)
        TextView xqtitle;
        @BindView(R.id.xqstartTime)
        TextView xqstartTime;
        @BindView(R.id.xqendTime)
        TextView xqendTime;
        @BindView(R.id.xqdel)
        TextView xqdel;
        @BindView(R.id.xqedit)
        TextView xqedit;
        @BindView(R.id.xqlook)
        TextView xqlook;

        public PublishXQViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
